package PageTable;

import comment.Comment;
import page.Page;
import utils.JdbcUtils;

import javax.swing.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

public class QueryPage {

    public static  void find(int power,String name){
        JFrame frame=  new JFrame("查询");
        String info = JOptionPane.showInputDialog(frame,"你要查询的文章名为:","查询",JOptionPane.WARNING_MESSAGE);
        List<Map<String, Object>> re= JdbcUtils.JdbcQuery(Page.sql_query_id,info);
        if(!re.isEmpty()){
            JOptionPane.showMessageDialog(frame,"查询成功");
            //设置窗体大小
            frame.setSize(800, 600);
            //设置窗体居中
            frame.setLocationRelativeTo(null);
            //取消布局
            frame.setLayout(null);
            //设置窗口不能改变大小
            frame.setResizable(false);
            //设置窗体是否可见
            frame.setVisible(true);
            int id=0;
            String name_page = null;
            String class_ = null;
            String content = null;
            LocalDateTime betime = null;
            LocalDateTime updatetime = null;
            String username = null;
            for(Map<String,Object> map : re){
                for(String key : map.keySet()) {
                    if(key.equals("page_id"))id= (int) map.get(key);
                    if (key.equals("page_name")) name_page = (String) map.get(key);
                    if (key.equals("page_class")) class_ = (String) map.get(key);
                    if (key.equals("page_content")) content = (String) map.get(key);
                    if (key.equals("page_releasedata")) betime = (LocalDateTime) map.get(key);
                    if (key.equals("page_update")) updatetime = (LocalDateTime) map.get(key);
                    if (key.equals("page_permissions")) power = (int) map.get(key);
                    if(key.equals("page_user"))username= (String) map.get(key);
                }
            }
            Box textid=Box.createHorizontalBox();
            JLabel textid_a = new JLabel("文章id为:"+id);
            textid.add(textid_a);
            textid.add(Box.createHorizontalStrut(10));
            textid.setBounds(30,50,700,30);
            frame.add(textid);

            Box textuser=Box.createHorizontalBox();
            JLabel textuser_a=new JLabel("文章作者为:"+username);
            textuser.add(textuser_a);
            textuser.add(Box.createHorizontalStrut(10));
            textid.setBounds(30,100,700,30);
            frame.add(textid);

            Box texttimebe=Box.createHorizontalBox();
            JLabel texttimebe_a = new JLabel("发布时间id为:"+betime);
            texttimebe.add(texttimebe_a);
            texttimebe.add(Box.createHorizontalStrut(10));
            texttimebe.setBounds(30,150,700,30);
            frame.add(texttimebe);

            Box texttime=Box.createHorizontalBox();
            JLabel texttime_a = new JLabel("上次修改时间id为:"+updatetime);
            texttime.add(texttime_a);
            texttime.add(Box.createHorizontalStrut(10));
            texttime.setBounds(30,200,700,30);
            frame.add(texttime);

            Box textname=Box.createHorizontalBox();
            JLabel textname_a = new JLabel("文章名为为:"+name_page);
            textname.add(textname_a);
            textname.setBounds(30,250,700,30);
            frame.add(textname);

            Box textclass_=Box.createHorizontalBox();
            JLabel textclass_a = new JLabel("文章分类为:"+class_);
            textclass_.add(textclass_a);
            textclass_.setBounds(30,300,700,30);
            frame.add(textclass_);

            Box comment=Box.createHorizontalBox();
            JLabel comment_a = new JLabel("文章下的评论数为:"+ JdbcUtils.JdbcQuery(Comment.sql_queryComment_sum,name_page).size());
            comment.add(comment_a);
            comment.setBounds(30,350,700,30);
            frame.add(comment);

            Box textcontent=Box.createHorizontalBox();
            JTextArea textcontent_a = new JTextArea(2,30);
            textcontent_a.append("原文为:\n"+content);
            textcontent_a.setLineWrap(true);// 激活自动换行功能
            textcontent_a.setEditable(false); //只读状态
            JScrollPane croll1 = new JScrollPane(textcontent_a);
            textcontent.add(croll1);
            textcontent.setBounds(30,400,700,80);
            frame.add(textcontent);
        }else{
            JOptionPane.showMessageDialog(frame,"查询失败");
        }
    }
}
